package gdcp.demo_4.cn;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class EditActivity extends AppCompatActivity {
    private EditText edtName;
    private EditText edtAge;
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private RadioButton radioButton3;
    private RadioButton radioButton4;
    private RadioButton radioButton5;
    private RadioButton radioButton6;
    private RadioButton radioButton7;
    private Button btnSave;
    private String no;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        setTitle("编辑学生");
        findView();

        initViewData();

        setBtnSaveClickListener();
    }


    /**
     * 设置保存按钮的点击事件
     */
    private void setBtnSaveClickListener() {
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = edtName.getText().toString();
                int age = Integer.parseInt(edtAge.getText().toString());

                int imgId = R.drawable.img1;

                if (radioButton1.isChecked()) {
                    imgId = R.drawable.img1;
                } else if (radioButton2.isChecked()) {
                    imgId = R.drawable.img2;
                } else if (radioButton3.isChecked()) {
                    imgId = R.drawable.img3;
                } else if (radioButton4.isChecked()) {
                    imgId = R.drawable.img4;
                } else if (radioButton5.isChecked()) {
                    imgId = R.drawable.img5;
                } else if (radioButton6.isChecked()) {
                    imgId = R.drawable.img6;
                } else if (radioButton7.isChecked()) {
                    imgId = R.drawable.img7;
                }

                Intent intent = new Intent();
                intent.putExtra("NO",no);
                intent.putExtra("NAME", name);
                intent.putExtra("AGE", age);
                intent.putExtra("IMG", imgId);
                setResult(3001, intent);

                finish();

            }
        });
    }

    /**
     *初始化界面数据
     */
    private void initViewData(){
        setTitle("编写学生");
        Intent intent = getIntent();
        String name = intent.getStringExtra("NAME");
        int age = intent.getIntExtra("AGE", 0);
        int imgId = intent.getIntExtra("IMFID", R.drawable.img7);
        no = intent.getStringExtra("NO");

        edtName.setText(name);
        edtAge.setText(String.valueOf(age));

        switch (imgId){
            case R.drawable.img1:
                radioButton1.setChecked(true);
                break;

            case R.drawable.img2:
                radioButton2.setChecked(true);
                break;
            case R.drawable.img3:
                radioButton3.setChecked(true);
                break;
            case R.drawable.img4:
                radioButton4.setChecked(true);
                break;
            case R.drawable.img5:
                radioButton5.setChecked(true);
                break;
            case R.drawable.img6:
                radioButton6.setChecked(true);
                break;
            case R.drawable.img7:
                radioButton7.setChecked(true);
                break;

        }
    }

    /**
     *绑定各自的组件
     */
    private void findView(){
        edtName = findViewById(R.id.edtName);
        edtAge = findViewById(R.id.edtAge);

        radioButton1 = findViewById(R.id.radioBtn1);
        radioButton2 = findViewById(R.id.radioBtn2);
        radioButton3 = findViewById(R.id.radioBtn3);
        radioButton4 = findViewById(R.id.radioBtn4);
        radioButton5 = findViewById(R.id.radioBtn5);
        radioButton6 = findViewById(R.id.radioBtn6);
        radioButton7 = findViewById(R.id.radioBtn7);
        btnSave = findViewById(R.id.btn_save);
    }
}



